<template>
  <div>
    <div class="Searchbox">
      <el-card class="box-card" shadow="never">
        <el-form ref="form" :model="form" label-width="120px">
          <el-row :gutter="20">
            <el-col :span="4">
              <el-form-item label="人员名称">
                <el-input v-model="seleceDeptToPage.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button type="primary" @click="getCompanyToPages(1)" icon="el-icon-search">搜索</el-button>
            </el-col>

            <el-col :span="4">
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true,flag=1,disabled= false,AddDeptData={},title='添加人员'">添加</el-button>
            </el-col>
          </el-row>
          <!-- 
          <el-row :gutter="4">
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible=true,flag=1,disabled= false,AddDeptData={},title='添加职能部门'">添加</el-button>
          </el-row>-->
        </el-form>
      </el-card>
      <br />
      <el-card class="box-card" shadow="never">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="loginName" label="登录名" width="250"></el-table-column>
          <el-table-column prop="name" label="姓名" width="200"></el-table-column>
          <el-table-column prop="phone" label="手机号" width="200"></el-table-column>
          <el-table-column prop="deptName" label="部门名称" width="200"></el-table-column>
          <el-table-column label="部门类型">
            <template slot-scope="scope">
              <span v-html="scope.row.role=='3'?'城市公司':'职能部门'"></span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="250">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row.id , scope.row.role),disabled = true" type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="eidtData(scope.row),title='编辑人员'">编辑</el-button>
              <el-button @click="doDeleteDept(scope.row.id)" type="text" size="small">删除</el-button>
              <el-button @click="updatepassword(scope.row)" type="text" size="small">修改密码</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagebox">
          <el-pagination background layout="prev, pager, next" :page-size="seleceDeptToPage.pageSize" :total="dataCount" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </el-card>
    </div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-position="right" :disabled="disabled">
        <el-form-item label="登陆名" label-width="100px">
          <el-input v-model="AddDeptData.loginName" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item v-if="flag==1" label="登陆密码" label-width="100px">
          <el-input v-model="AddDeptData.password" style="width:200px;" show-password></el-input>
        </el-form-item>

        <el-form-item label="姓名" label-width="100px">
          <el-input v-model="AddDeptData.name" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="手机号" label-width="100px">
          <el-input v-model="AddDeptData.phone" style="width:200px;"></el-input>
        </el-form-item>

        <el-form-item label="部门类型" label-width="100px">
          <el-radio-group v-model="AddDeptData.role" @change="dorole()" >
            <el-radio label="2">职能部门</el-radio>
            <el-radio label="3">城市公司</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="AddDeptData.role=='2'" label="部门名称" label-width="100px">
            <el-select v-if="AddDeptData.role=='2'" style="width:200px;" v-model="deptId" @change="getdeptId()" placeholder="职能部门">
              <el-option :label="item.deptName" :value="item.id" :key="item.id" v-for="item in DeptToPage"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item v-if="AddDeptData.role=='3'" label="城市公司名称" label-width="100px">
          <el-select v-if="AddDeptData.role=='3'" style="width:200px;" v-model="deptId" @change="getdeptId()" placeholder="城市公司">
            <el-option :label="item.companyName" :value="item.id" :key="item.id" v-for="item in CompanyToPages"></el-option>
          </el-select>
        </el-form-item>

        
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false,AddDeptData = {}" v-if="!disabled">取 消</el-button>
        <el-button type="primary" v-if="flag==2" @click="dialogFormVisible = false">确 定</el-button>
        <el-button type="primary" v-if="flag==1" @click="getaddCompany()">确 定</el-button>
        <el-button type="primary" v-if="flag==3" @click="doupdateDeptMessage()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改密码" :visible.sync="passwordShow" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="新密码">
          <el-input v-model="DoPassWord.newPassWord" show-password style="width:300px;"></el-input>
        </el-form-item>

        <el-form-item label="确认密码">
          <el-input v-model="DoPassWord.oldPassWord" show-password style="width:300px;"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="passwordShow = false">取 消</el-button>
        <el-button type="primary" @click="doupdateUserPassWord">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addUser,
  updateUserPassWord,
  updateUserMessage,
  getUserToPage,
  getAllUserToList,
  DeleteUser,
  getUserByDeptId,
  getUserByUserId,
  getAllDeptToList,
  getAllCompanyToList,
} from "@/api/api";

export default {
  data() {
    return {
      title: "",
      flag: 0,
      dialogFormVisible: false,
      passwordShow: false,
      deptId:"",
   
      disabled: true,
      seleceDeptToPage: {
        name: "",
        pageIndex: 1,
        pageSize: 10,
      },
      form: {},
      AddDeptData: {
        deptId:""
      },
      tableData: [],
      dataCount: 0,

      DeptToPage: [],
      CompanyToPages: [],

      DoPassWord: {
        oldPassWord: "",
        newPassWord: "",
      },
    };
  },
  mounted() {
    let that = this;
    that.getCompanyToPages(1);

    that.getAllDeptToLists();
    that.getAllCompanyToLists();
  },

  methods: {

    getdeptId(){
      let that = this
      that.AddDeptData.deptId = that.deptId
    },

    dorole(){
      let that = this
      that.AddDeptData.deptId = ""

    },
    updatepassword(row) {
      let that = this;
      that.passwordShow = true;
      that.DoPassWord.id = row.id;
      that.DoPassWord.oldPassWord = row.oldPassWord;
      that.DoPassWord.newPassWord = row.newPassWord;
    },

    async doupdateUserPassWord() {
      let that = this;

      if (that.DoPassWord.oldPassWord !== that.DoPassWord.newPassWord) {
        this.$message({
          message: "两次密码输入不一致!",
          type: "warning",
        });
        return;
      }
      let pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[~!@#$%^&*]).{12,20}');
      if(!pwdRegex.test(that.DoPassWord.newPassWord)){
         this.$message({
          message: "密码必须包含字母、数字、特殊字符最少12位,最长20位!",
          type: "warning",
        });
        return;
      }

      let params = {
        newPassWord: that.DoPassWord.newPassWord,
        id: that.DoPassWord.id,
      };
      let res = await updateUserPassWord(params);
      if (res.data.Code == 200) {
        that.$message.success(res.data.Msg);
        that.passwordShow = false;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async getAllDeptToLists() {
      let that = this;
      let params = {};
      let res = await getAllDeptToList(params);
      if (res.data.Code == 200) {
        that.DeptToPage = res.data.Data;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async getAllCompanyToLists() {
      let that = this;
      let params = {};
      let res = await getAllCompanyToList(params);
      if (res.data.Code == 200) {
        that.CompanyToPages = res.data.Data;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async doDeleteDept(id) {
      let that = this;
      let params = {
        id: id,
      };
      let res = await DeleteUser(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    eidtData(row) {
      let that = this;
      that.dialogFormVisible = true;
      that.AddDeptData = row;
      that.flag = 3;
      that.disabled = false;
    },

    async doupdateDeptMessage() {
      let that = this;

      if (
        that.AddDeptData.loginName == undefined ||
        that.AddDeptData.loginName == ""
      ) {
        that.$message.warning("登陆名不能为空！");
        return;
      }

      if (that.AddDeptData.name == undefined || that.AddDeptData.name == "") {
        that.$message.warning("姓名不能为空！");
        return;
      }
      if (that.AddDeptData.role == undefined || that.AddDeptData.role == "") {
        that.$message.warning("部门类型不能为空！");
        return;
      }
      if (
        that.AddDeptData.deptId == undefined ||
        that.AddDeptData.deptId == ""
      ) {
        that.$message.warning("部门类型不能为空！");
        return;
      }

      let params = that.AddDeptData;
      let res = await updateUserMessage(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = !that.dialogFormVisible;
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    async handleClick(id, role) {
      let that = this;
      that.flag = 2;
      that.disabled = false;
      that.title = "查看人员信息";
      that.dialogFormVisible = !that.dialogFormVisible;
      let params = {
        id: id,
      };
      if (role == 2) {
        let res = await getUserByUserId(params);
        if (res.data.Code == 200) {
          that.AddDeptData = res.data.Data;
        } else if (res.data.Code == 400) {
          this.$message.warning(res.data.Msg);
        } else if (res.data.Code == 401) {
          this.$router.push({ path: "/login" });
        }
      } else if (role == 3) {
        let res = await getUserByUserId(params);
        if (res.data.Code == 200) {
          that.AddDeptData = res.data.Data;
        } else if (res.data.Code == 400) {
          this.$message.warning(res.data.Msg);
        } else if (res.data.Code == 401) {
          this.$router.push({ path: "/login" });
        }
      }
    },

    async getaddCompany() {
      let that = this;

      if (
        that.AddDeptData.loginName == undefined ||
        that.AddDeptData.loginName == ""
      ) {
        that.$message.warning("登陆名不能为空！");
        return;
      }

      if (
        that.AddDeptData.password == undefined ||
        that.AddDeptData.password == ""
      ) {
        that.$message.warning("密码不能为空！");
        return;
      }

      if (that.AddDeptData.name == undefined || that.AddDeptData.name == "") {
        that.$message.warning("姓名不能为空！");
        return;
      }
      if (that.AddDeptData.role == undefined || that.AddDeptData.role == "") {
        that.$message.warning("部门类型不能为空！");
        return;
      }
      if (
        that.AddDeptData.deptId == undefined ||
        that.AddDeptData.deptId == ""
      ) {
        that.$message.warning("部门类型不能为空！");
        return;
      }

      let params = that.AddDeptData;
      let res = await addUser(params);
      if (res.data.Code == 200) {
        this.$message.success(res.data.Msg);
        that.dialogFormVisible = false;
        that.getCompanyToPages(that.seleceDeptToPage.pageIndex);
      } else if (res.data.Code == 400) {
        this.$message.success(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },

    handleCurrentChange(val) {
      let that = this;
      that.seleceDeptToPage.pageIndex = val;
      that.getCompanyToPages(val);
    },

    async getCompanyToPages(index) {
      let that = this;
      that.seleceDeptToPage.pageIndex = index;
      let params = that.seleceDeptToPage;
      let res = await getUserToPage(params);
      if (res.data.Code == 200) {
        that.tableData = res.data.Data;
        that.dataCount = res.data.dataCount;
      } else if (res.data.Code == 400) {
        this.$message.warning(res.data.Msg);
      } else if (res.data.Code == 401) {
        this.$router.push({ path: "/login" });
      }
    },
  },
};
</script>

<style scoped>
.Searchbox {
}
</style>